$(function () {
    var arr=["list,D0707,D0708,D0709,total","万焕锡 , 3 , 0 , 0 , 3","余艳 , 0 , 1 , 0 , 1","刘家明 , 1 , 1 , 0 , 2","唐爱国 , 0 , 1 , 1 , 2","施金珠 , 0 , 0 , 38 , 38","朱凯 , 2 , 0 , 0 , 2","李卫增 , 0 , 1 , 1 , 2","李培猛 , 0 , 0 , 5 , 5","李翰明 , 1 , 1 , 0 , 2","李荣秀 , 1 , 0 , 1 , 2","杨琪 , 1 , 0 , 0 , 1","林旭又 , 0 , 1 , 1 , 2","潘美香 , 0 , 0 , 1 , 1","甘世仕 , 0 , 2 , 1 , 3","甘美荣 , 3 , 0 , 6 , 9","罗华峻 , 4 , 0 , 0 , 4","蓝彩凤 , 0 , 1 , 0 , 1","蓝鹏 , 0 , 13 , 0 , 13","覃桃 , 2 , 5 , 0 , 7","赵流金 , 1 , 1 , 1 , 3","邓杰民 , 0 , 1 , 0 , 1","陆家第 , 0 , 2 , 1 , 3","陆爱梅 , 1 , 5 , 12 , 18","陆生桂 , 1 , 1 , 1 , 3","陆艳芳 , 0 , 1 , 1 , 2","韦春耐 , 0 , 0 , 1 , 1","黄国志 , 2 , 0 , 0 , 2","黄文江 , 1 , 2 , 5 , 8","total , 24 , 40 , 77 , 141"];
   /* for(var i=0;i<arr.lenth;i++){
        console.log(arr[i]);
    }*/
    $.each(arr,function(index,item){

        console.log(item.split(","));
        $.each(item.split(","),function(index,item){
            console.log(item);
        });
    });
    $.get("../wxuserpersonal/person/list", function(r){
        vm.personlist = r.listPerson;


    });
    $.get("../wxuserpersonal/wxuser/list", function(r){
        vm.wxuserlist = r.listWxUser;
        var list = r.listWxUser;
        var html='';
        html='<li class="list-group-item">班主列表</li>';
        $.each(list,function(index,item){
            html+='<li class="list-group-item"  onclick="show_sub('+item.id+')">'+item.nickname+'</li>';
        });
        $("#wxuser").html(html);

    });

    $.get("../wxuserpersonal/groupPersonnel/list", function(r){
        vm.groupPersonnel = r.listGroupPersonnel;
        var list = r.listGroupPersonnel;
        var html='';

        html+='<option value="0">请选择分组</option>';
        $.each(list,function(index,item){

            html+='<option  value="'+item.id+'">'+item.name+'</option>';

        });
        $("#group").html(html);

    });

    $("#jqGrid").jqGrid({
        url: '../wxuserpersonal/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', width: 50, key: true },
			{ label: '下属员工', name: 'idPersonal', width: 80, formatter:customFmatter },
			{ label: '班主', name: 'idWeixinUser', width: 80, formatter:customFmatter1 }
        ],
		viewrecords: true,
           height: 'auto',
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

function customFmatter(cellvalue, options, rowObject){

    var name;

    $.each(vm.wxuserlist,function(index,item){

        if(cellvalue==item.id){

            name=item.name;
        }

    });


    return name;
}

function customFmatter1(cellvalue, options, rowObject){

    var name;

    $.each(vm.personlist,function(index,item){

        if(cellvalue==item.id){

            name=item.name;
        }

    });

    //alert(name);
    return name;
}

function show_sub(v){

    vm.showList = true;
    vm.idWxUser=v;
    var page = $("#jqGrid").jqGrid('getGridParam','page');
    $("#jqGrid").jqGrid('setGridParam',{
        postData:{'idWxUser': v},
        page:page
    }).trigger("reloadGrid");
}
function show_sub_person(idGroup){
  vm.idGroup=idGroup;
}
function getval(){
    var name=$("#name").val();

    $.get("../wxuserpersonal/person/list/"+vm.idGroup+"?str="+name, function(r){
        vm.personlist = r.listPerson;
        var list = r.listPerson;
        var html='';
        $.each(list,function(index,item){
            html+=' <div class="col-md-3">'
                +' <div class="input-group">'
                +' <span class="input-group-addon">'
                +'<input type="checkbox" name="person_checkbox" id="'+item.id+'">'+item.name+'||'+item.codeTag+''
                +'</span>'

                +' </div>'
                +' </div>';

        });
        $("#person").html(html);

    });
}

function checkAll(XelementName){
    var checkboxes=document.getElementsByName(XelementName);
    for(var i=0;i<checkboxes.length;i++)
    {
        checkboxes[i].checked=true;
    }
}
function checkNotAll(XelementName){
    var checkboxes=document.getElementsByName(XelementName);
    for(var i=0;i<checkboxes.length;i++)
    {
        checkboxes[i].checked=false;
    }
}


function addSome(){


    var list=new Array();
    $("input[name='person_checkbox']:checkbox:checked").each(function(index,item){
        var idPerson=$(this).attr("id");
        var entity=new Object();
        entity.idPersonal=idPerson;
        entity.idWeixinUser=vm.idWxUser;

        list.push(entity);
    }) ;
    console.log(JSON.stringify(list));
    var url="../wxuserpersonal/addBatch";
    $.ajax({
        type: "POST",
        url: url,
        dataType:"json",
        contentType:"application/json",
        data: JSON.stringify(list),
        success: function(r){
            if(r.code === 0){
                alert('操作成功', function(index){
                    history.go(0);
                });
            }else{
                alert(r.msg);
            }
        }
    });
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		wxuserPersonal: {},
        wxuserlist:{},
        personlist:{},
        idGroup:{},
        idWxUser:{}

	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
            if(JSON.stringify(vm.idWxUser)=="{}"){
                alert("请先选择班主");
                return ;
            }
			vm.showList = false;
			vm.title = "新增";
			vm.wxuserPersonal = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.wxuserPersonal.id == null ? "../wxuserpersonal/save" : "../wxuserpersonal/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.wxuserPersonal),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../wxuserpersonal/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../wxuserpersonal/info/"+id, function(r){
                vm.wxuserPersonal = r.wxuserPersonal;
            });
		},
        addBatch:function(){
            if(JSON.stringify(vm.idWxUser)=="{}"){
                alert("请先选择班主");
                return ;
            }
            $('#form1').hide();
            $('#form2').hide();
            $('#form3').show();

        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});